using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class BreakMakerItem : BaseItem
	{
		private enum BreakState
		{
			Static = 0,
			Completed = 1
		}

		private enum Anim
		{
			Static = 0,
			Up = 1
		}

		private float UP_POSITION = 0.3f;

		private BreakState breakState;

		private Anim anim;

		private float animTimer;

		private Transform smokeEfc;

		public bool isBurn;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			breakState = (BreakState)data.state;
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (anim == Anim.Up)
			{
				animTimer += Time.deltaTime * 5f;
				for (int i = 0; i < childItems.Count; i++)
				{
					if (childItems[i].itemData.canBurn)
					{
						childItems[i].SetLocalPosition(0f, Mathf.Lerp(0f, UP_POSITION, animTimer));
					}
				}
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
				}
			}
			if (!isBurn || breakState == BreakState.Completed)
			{
				return;
			}
			animTimer += Time.deltaTime;
			if (animTimer >= 3f)
			{
				breakState = BreakState.Completed;
				anim = Anim.Up;
				animTimer = 0f;
				Singleton<AudioManager>.Instance.PlaySound("sound_break_maker_2");
			}
			for (int j = 0; j < childItems.Count; j++)
			{
				if (childItems[j].itemData.canBurn)
				{
					childItems[j].Burn(Time.deltaTime * 2.5f);
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (item.itemData.canBurn)
			{
				IsBurn(true);
				item.StartBurn();
				Singleton<AudioManager>.Instance.PlaySound("sound_break_maker_1");
			}
			anim = Anim.Static;
			animTimer = 0f;
			bool result = base.AttachItem(item, depthType, attachType, updatePosition);
			if (updatePosition && breakState == BreakState.Completed)
			{
				item.SetLocalPosition(0f, UP_POSITION);
			}
			return result;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			if (isBurn && item.itemData.canBurn)
			{
				IsBurn(false);
			}
			breakState = BreakState.Static;
			anim = Anim.Static;
			base.ReleaseAttachItem(item);
		}

		private void ShowSmokeEffect()
		{
			if (string.IsNullOrEmpty(base.itemData.effect))
			{
				return;
			}
			if (smokeEfc == null)
			{
				Transform transform = model.Find("Efc");
				if (transform != null)
				{
					smokeEfc = Singleton<GamePoolManager>.Instance.Spawn(PathConst.EFFECT + base.itemData.effect);
					if (smokeEfc != null)
					{
						smokeEfc.SetParent(transform);
						smokeEfc.localPosition = Vector3.zero;
						smokeEfc.localScale = Vector3.one;
					}
				}
			}
			SetEffectLoop(smokeEfc, true);
		}

		public void IsBurn(bool val)
		{
			if (isBurn != val)
			{
				isBurn = val;
				if (isBurn)
				{
					ShowSmokeEffect();
				}
				else
				{
					SetEffectLoop(smokeEfc, false);
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)breakState;
		}
	}
}
